package controllers

import (
	"GO_BACK/api/dbapi"
	"GO_BACK/model/data"
	"fmt"
	"github.com/gin-gonic/gin"
	"net/http"
)

func RegisterController(c *gin.Context) {
	var resget *data.RegisterInfo
	err := c.BindJSON(&resget)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"message": "错误",
			"err":     err,
		})
	} else {
		fmt.Println("1,resget:", resget)
		if dbapi.QueryByVertifyCode(resget.Vertifycode) {
			user := &data.PostgresUserInfo{
				Name:     resget.Name,
				Password: resget.Password,
				Email:    resget.Email,
			}
			db, _ := dbapi.PgDbConnect()
			fmt.Println("2,user:", user)
			result := *dbapi.PgDbCreate(user, db)
			fmt.Println("result.RowsAffected", result.RowsAffected)
			if result.RowsAffected > 0 {
				info := dbapi.Querybyemail(user.Email)
				fmt.Println("info:", &info)
				c.JSON(http.StatusOK, gin.H{
					"message": "注册成功",
					"info":    info,
				})
			} else {
				c.JSON(http.StatusBadRequest, gin.H{
					"message": "注册失败",
					"info":    result,
				})
			}

		} else {
			c.JSON(http.StatusBadRequest, gin.H{
				"message": "注册失败",
				"info":    "",
			})
		}

	}

}
